﻿using Digitalmes.Hubs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;

namespace Digitalmes.Areas.MessageManagement
{
    [Public]
    [ActionDescription("_Group._Default")]
    [Route("/api/MessageManagement/Message")]
    public class MessageController : BaseApiController
    {
        private readonly IHubContext<ChatHub> _hubContext;

        public MessageController(IHubContext<ChatHub> hubContext)
        {
            _hubContext = hubContext;
        }

        [Public]
        // 获取群组列表API
        [HttpGet("GetGroups")]
        public IActionResult GetGroups()
        {
            // 实际项目中可以从数据库获取
            var groups = new List<GroupVM>
            {
                new GroupVM { Id = 1, Name = "技术交流", MemberCount = 12 },
                new GroupVM { Id = 2, Name = "产品反馈", MemberCount = 8 },
                new GroupVM { Id = 3, Name = "日常闲聊", MemberCount = 20 },
                new GroupVM { Id = 4, Name = "项目协作", MemberCount = 5 }
            };

            return Ok(groups);
        }

        [Public]
        // 获取历史消息API
        [HttpGet("GetHistoryMessages")]
        public IActionResult GetHistoryMessages([FromBody] MessageRequest request)
        {
            // 实际项目中可以从数据库获取历史消息
            var messages = new List<MessageVM>();

            // 模拟数据
            if (request.GroupId == 1)
            {
                messages.Add(new MessageVM
                {
                    Sender = "系统",
                    Content = "欢迎来到技术交流群组！",
                    Time = "10:30",
                    IsSystem = true
                });

                messages.Add(new MessageVM
                {
                    Sender = "李四",
                    Content = "有人吗？",
                    Time = "10:32"
                });
            }

            return Ok(messages);
        }

        [Public]
        // 发送系统通知API
        [HttpPost("SendSystemNotification")]
        [ActionDescription("发送系统通知")]
        public async Task<IActionResult> SendSystemNotification([FromBody] SystemNotificationVM notification)
        {
            if (string.IsNullOrEmpty(notification.TargetGroup))
            {
                // 发送给所有用户
                await _hubContext.Clients.All.SendAsync("ReceiveMessage", "系统",
                    notification.Content, DateTime.Now.ToString("HH:mm"));
            }
            else
            {
                // 发送给特定群组
                await _hubContext.Clients.Group(notification.TargetGroup).SendAsync("ReceiveMessage",
                    "系统", notification.Content, DateTime.Now.ToString("HH:mm"));
            }

            return Ok(new { Success = true, Message = "通知发送成功" });
        }
    }

    public class GroupVM
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int MemberCount { get; set; }
    }

    public class MessageVM
    {
        public string Sender { get; set; }
        public string Content { get; set; }
        public string Time { get; set; }
        public bool IsSystem { get; set; }
    }

    public class MessageRequest
    {
        public int GroupId { get; set; }
        public int Page { get; set; } = 1;
        public int PageSize { get; set; } = 20;
    }

    public class SystemNotificationVM
    {
        public string TargetGroup { get; set; }
        public string Content { get; set; }

        public string Time { get; set; }    
    }
}

